﻿using System;
using CustomerContactSystem.Core.Infrastructure.Domain;
using CustomerContactSystem.Core.Infrastructure.Email;

namespace CustomerContactSystem.Core.Model {
    public class EnquiryEmail : Entity {
        public virtual Enquiry Enquiry { get; set; }
        public virtual DateTime DateCreated { get; set; }
        public virtual string SentFrom { get; set; }
        public virtual string SentTo { get; set; }
        public virtual string CcList { get; set; }
        public virtual string BccList { get; set; }
        public virtual string Subject { get; set; }
        public virtual string EmailBody { get; set; }
        public virtual string Attachments { get; set; }
        public virtual bool IsHtml { get; set; }
        public virtual bool HasBeenSent { get; set; }
        public virtual DateTime? DateSent { get; set; }

        public static EnquiryEmail CommentAddedEmail(string sentFrom, string sentTo, string addedBy, string enquiryDetail, string commentDetail, string enquiryViewUrl) {
            var enquiryEmail = new EnquiryEmail
                {
                    DateCreated = DateTime.Now,
                    IsHtml = true,
                    HasBeenSent = false,
                    SentFrom = sentFrom,
                    SentTo = sentTo,
                    Subject = "Comment added to Enquiry",
                    EmailBody =
                        "<p>A comment has been added by " + addedBy +
                        " to an enquiry you submitted or are involved in.</p>"
                };

            enquiryEmail.EmailBody += "<p><strong>Enquiry:</strong>" + enquiryDetail + "</p>";
            enquiryEmail.EmailBody += "<p><strong>Comment:</strong>" + commentDetail + "</p>";
            enquiryEmail.EmailBody += "<p><a href=\"" + enquiryViewUrl + "\">View Enquiry</a></p>";
            return enquiryEmail;
        }

        public static EnquiryEmail DocumentAddedEmail(string sentFrom, string sentTo, string addedBy, string enquiryDetail, string documentUrl, string enquiryViewUrl) {
            var enquiryEmail = new EnquiryEmail
            {
                DateCreated = DateTime.Now,
                IsHtml = true,
                HasBeenSent = false,
                SentFrom = sentFrom,
                SentTo = sentTo,
                Subject = "Document added to Enquiry",
                EmailBody =
                    "<p>A document has been added by " + addedBy +
                    " to an enquiry you submitted or are involved in.</p>"
            };

            enquiryEmail.EmailBody += "<p><strong>Enquiry:</strong>" + enquiryDetail + "</p>";
            enquiryEmail.EmailBody += "<p><strong>Document:</strong> <a href=\"" + documentUrl + "\">View</a></p>";
            enquiryEmail.EmailBody += "<p><a href=\"" + enquiryViewUrl + "\">View Enquiry</a></p>";
            return enquiryEmail;
        }

        public static EnquiryEmail ReferralEmail(string sentFrom, string sentTo, string enquiryViewUrl, string referredBy, string customer, string enquiryDetail, DateTime deadline) {
            var enquiryEmail = new EnquiryEmail
            {
                DateCreated = DateTime.Now,
                IsHtml = true,
                HasBeenSent = false,
                SentFrom = sentFrom,
                SentTo = sentTo,
                Subject = "Enquiry Referred to You",
                EmailBody =
                    "<p>You have been referred an enquiry by " + referredBy +
                    ". The details are as follows:-.</p>"
            };

            enquiryEmail.EmailBody += "<p><strong>Customer:</strong>" + customer + "</p>";
            enquiryEmail.EmailBody += "<p><strong>Detail:</strong>" + enquiryDetail + "</p>";
            enquiryEmail.EmailBody += "<p><strong>Deadline:</strong>" + deadline.ToShortDateString() + "</p>";
            enquiryEmail.EmailBody += "<p><a href=\"" + enquiryViewUrl + "\">View Enquiry</a></p>";
            return enquiryEmail;
        }

        public virtual BaseEmail ToEmail() {
            var baseEmail = new BaseEmail
                {
                    IsHtml = IsHtml,
                    Recipient = SentTo,
                    SentFrom = SentFrom,
                    Subject = Subject
                };
            baseEmail.Write(EmailBody);

            if (!string.IsNullOrEmpty(CcList)) {
                var ccEmails = CcList.Split(';');
                foreach (var ccEmail in ccEmails) {
                    baseEmail.AddCcAddress(ccEmail);
                }
            }

            if (!string.IsNullOrEmpty(BccList)) {
                var bccEmails = BccList.Split(';');
                foreach (var bccEmail in bccEmails) {
                    baseEmail.AddBccAddress(bccEmail);
                }
            }

            if (!string.IsNullOrEmpty(Attachments)) {
                var attachments = Attachments.Split(';');
                foreach (var attachment in attachments) {
                    baseEmail.AddAttachment(attachment);
                }
            }

            return baseEmail;
        }

        protected override void Validate() {
            if (Enquiry == null) {
                AddBrokenRule(new BusinessRule("Enquiry", "Enquiry is required!"));
            }

            if (string.IsNullOrEmpty(SentFrom)) {
                AddBrokenRule(new BusinessRule("SentFrom", "Sent from is required!"));
            }

            if (string.IsNullOrEmpty(SentTo)) {
                AddBrokenRule(new BusinessRule("SentTo", "Sent to is required!"));
            }

            if (string.IsNullOrEmpty(Subject)) {
                AddBrokenRule(new BusinessRule("Subject", "Subject is required!"));
            }

            if (string.IsNullOrEmpty(EmailBody)) {
                AddBrokenRule(new BusinessRule("EmailBody", "Email body is required!"));
            }
        }
    }
}